const sql = require('mssql');
const baseFn = require('../../public/base.js');

const getCompanyInfo = async (ctx, next) => {

    let selectSql = `SELECT * FROM GSGL`;

    let result = await sql.query(selectSql);
    ctx.body = { result: result.recordset };
}
// 添加公司信息
const addcompany = async (ctx, next) => {
    let getBody = ctx.request.body;
    let actionSql = '';
    if (('btn' in getBody.parantInfo) && getBody.parantInfo.btn === 'edit') {
        actionSql = `UPDATE GSGL SET name = '${getBody.name}',category = ${getBody.category} WHERE id = ${getBody.parantInfo.id}`;
    } else {
        actionSql = `INSERT INTO GSGL(name,type,pid,status,category) VALUES ('${getBody.name}',${('type' in getBody.parantInfo) ? (Number(getBody.parantInfo.type) + 1) : 1},${('pid' in getBody.parantInfo) ? (getBody.parantInfo.id) : 0},'T',${getBody.category} )`;
    }
    let result = await sql.query(actionSql);
    if (result.rowsAffected[0] > 0) {
        ctx.body = {
            success: true
        }
    } else {
        ctx.body = {
            success: false
        }
    }
}
// 改变公司的使用状态
const changeCompanyStauts = async (ctx, next) => {
    let getBody = ctx.request.body;
    getBody.forEach(async item => {
        await sql.query(`UPDATE GSGL SET status = '${getBody[0].status === 'T'?'F':'T'}' WHERE id = ${item.id}`);
    });
   
        ctx.body = {
            success: true
        }
    
}


module.exports = {
    'POST /changeCompanyStauts': changeCompanyStauts,
    'POST /addcompany': addcompany,
    'GET /getCompanyInfo': getCompanyInfo
}